# -*- coding: utf-8 -*-
"""
基础组件类 - TgComponentMixin 和 TgBaseComponent
提供所有TG组件的基础功能
"""
from abc import abstractmethod
from PySide6.QtCore import QObject, Signal
from ...style_manager import StyleManager


class TgComponentMixin:
    """TG组件混入类 - 提供通用功能"""
    
    def __init__(self):
        self._component_name = self.__class__.__name__
        self._is_initialized = False
        self._style_applied = False
        self._theme = 'default'
        self._size_hint = 'medium'
    
    @property
    def component_name(self):
        """获取组件名称"""
        return self._component_name
    
    @property
    def is_initialized(self):
        """检查组件是否已初始化"""
        return self._is_initialized
    
    @property
    def style_applied(self):
        """检查样式是否已应用"""
        return self._style_applied
    
    @property
    def theme(self):
        """获取主题"""
        return getattr(self, '_theme', 'default')
    
    @theme.setter
    def theme(self, value):
        """设置主题"""
        if getattr(self, '_theme', None) != value:
            self._theme = value
            if hasattr(self, 'apply_style'):
                self.apply_style()
    
    @property
    def size_hint(self):
        """获取尺寸提示"""
        return getattr(self, '_size_hint', 'medium')
    
    @size_hint.setter
    def size_hint(self, value):
        """设置尺寸提示"""
        if getattr(self, '_size_hint', None) != value:
            self._size_hint = value
            if hasattr(self, 'apply_style'):
                self.apply_style()
    
    def setup_ui(self):
        """设置用户界面 - 子类应该重写此方法"""
        pass
    
    def apply_style(self):
        """应用样式 - 子类应该重写此方法"""
        pass
    
    def initialize(self):
        """初始化组件"""
        if not self._is_initialized:
            self.setup_ui()
            self.apply_style()
            self._is_initialized = True
            self._style_applied = True
    
    def get_style_properties(self):
        """获取样式属性字典"""
        return {
            'theme': self.theme,
            'size_hint': self.size_hint,
            'component_name': self._component_name
        }


class TgBaseComponent(QObject):
    """所有TG组件的基类 - 用于纯组件"""
    
    # 组件状态变化信号
    state_changed = Signal(str, object)  # 信号：状态名，新值
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self._component_name = self.__class__.__name__
        self._is_initialized = False
        self._style_applied = False
    
    @property
    def component_name(self):
        """获取组件名称"""
        return self._component_name
    
    @property
    def is_initialized(self):
        """检查组件是否已初始化"""
        return self._is_initialized
    
    @property
    def style_applied(self):
        """检查样式是否已应用"""
        return self._style_applied
    
    def setup_ui(self):
        """设置用户界面 - 子类应该重写此方法"""
        pass
    
    def apply_style(self):
        """应用样式 - 子类应该重写此方法"""
        pass
    
    def initialize(self):
        """初始化组件"""
        if not self._is_initialized:
            self.setup_ui()
            self.apply_style()
            self._is_initialized = True
            self._style_applied = True
    
    def emit_state_changed(self, state_name, value):
        """发射状态变化信号"""
        self.state_changed.emit(state_name, value) 